if (!exists("app_root"))
    {
    stop("Need to have app_root defined to source this template");
    };

source( paste( app_root, "cgi-lib/R/ep.io.R", sep="/" ) );
require( affy, keep.source = FALSE );

ep.affymeta = function( affydatafile, histpng, lineplotpng, heatmappng, legendpng )
    {
    load( affydatafile ); # loads 'affydata' object

    color = rainbow(length(affydata));

    # overall histogram
    png( filename = histpng, width = 500, height = 350, pointsize = 12 );
    par( mar= c(2.0, 2.0, 0.2, 0.2), mgp = c( 0.9, 0.1, 0 ), tcl = -0.25, cex.axis = 0.7 );
    hist( affydata, col = color[1:length(affydata)], lwd = 2, lty = "solid");
    dev.off();

    # intensity images [heatmaps]
    nr = ceiling(sqrt(length(affydata)));
    nc = ceiling(sqrt(length(affydata)));
    png( filename = heatmappng, width = nc * 250, height = nr * 250, pointsize = 12 );
    par( mfrow = c( nr, nc ) );
    for ( k in 1:length(affydata) )
        {
        image( affydata[,k], transfo = log, col.main = "white" );
        title( main = paste( sampleNames(affydata)[k], "chip surface intensity (log-scale)", sep="\n" ), cex.main = 1.0 );
        }
    dev.off();

    # overall boxplot
    png( filename = lineplotpng, width = 500, height = 350, pointsize = 12 );
    par( mar= c(2.0, 2.0, 0.2, 0.2), mgp = c( 0.9, 0.1, 0 ), tcl = -0.25, cex.axis = 0.7 );
    boxplot( affydata, col = color[1:length(affydata)], names = FALSE, ylim = c( 3, 16 ), xlab = "sample", ylab = "PM log intensities" );
    abline( h = c( 6, 7, 8 ), lty = c( 3, 2, 3 ) );
    dev.off();

    # legend
    png( filename = legendpng, width = 250, height = 350, pointsize = 12, colortype = "pseudo.cube" );
    plot.new();
    par( mar = c(0, 0, 0, 0), usr = c(0, 1, 0, 1) );
    legend( x = "topleft", inset = 0, legend = sampleNames(affydata), cex = 0.8, col = color[1:length(affydata)], lwd = 3, lty = "solid", bty="n");
    dev.off();

    return( c( cdfName(affydata), length(sampleNames(affydata)), length(geneNames(affydata)) ) );
    };
